package com.ruoyi.supplier.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.supplier.domain.TbPrice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.mapper.TbPriceStoreMapper;
import com.ruoyi.supplier.domain.TbPriceStore;
import com.ruoyi.supplier.service.ITbPriceStoreService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-10
 */
@Service
public class TbPriceStoreServiceImpl implements ITbPriceStoreService 
{
    public static final Logger log = LoggerFactory.getLogger(TbPriceStoreServiceImpl.class);

    @Autowired
    private TbPriceStoreMapper tbPriceStoreMapper;

    /**
     * 查询库存明细
     * 
     * @param id 库存明细ID
     * @return 库存明细
     */
    @Override
    public TbPriceStore selectTbPriceStoreById(Long id)
    {
        return tbPriceStoreMapper.selectTbPriceStoreById(id);
    }

    /**
     * 查询库存明细列表
     * 
     * @param tbPriceStore 库存明细
     * @return 库存明细
     */
    @Override
    public List<TbPriceStore> selectTbPriceStoreList(TbPriceStore tbPriceStore)
    {
        return tbPriceStoreMapper.selectTbPriceStoreList(tbPriceStore);
    }

    /**
     * 新增库存明细
     * 
     * @param tbPriceStore 库存明细
     * @return 结果
     */
    @Override
    public int insertTbPriceStore(TbPriceStore tbPriceStore)
    {

        return tbPriceStoreMapper.insertTbPriceStore(tbPriceStore);
    }

    /**
     * 修改库存明细
     * 
     * @param tbPriceStore 库存明细
     * @return 结果
     */
    @Override
    public int updateTbPriceStore(TbPriceStore tbPriceStore)
    {
        return tbPriceStoreMapper.updateTbPriceStore(tbPriceStore);
    }

    /**
     * 判断 商品信息是否存储在 库存明细中，如果存在，则更新，不存在，则插入
     * @param list
     * @return
     */
    @Override
    public int updateTbPriceStore(List<TbPriceStore> list) {
        // TODO 需要写判断逻辑


        int i=0;
        for (TbPriceStore tbPriceStore: list) {
            TbPriceStore tbPStore = tbPriceStoreMapper.selectTbPriceStoreByPriceIdAndStoreIdAndSid(tbPriceStore);
            if(tbPStore!=null){
                tbPriceStore.setUpdateTime(DateUtils.getNowDate());
                tbPriceStore.setId(tbPStore.getId());
                i+=tbPriceStoreMapper.updateTbPriceStore(tbPriceStore);
            }else{
                i+=tbPriceStoreMapper.insertTbPriceStore(tbPriceStore);
            }


        }
        return i;
    }

    /**
     * 批量删除库存明细
     * 
     * @param id 需要删除的库存明细ID
     * @return 结果
     */
    @Override
    public int deleteTbPriceStoreByIds(Long[] id)
    {
        return tbPriceStoreMapper.deleteTbPriceStoreByIds(id);
    }

    /**
     * 删除库存明细信息
     * 
     * @param priceId 库存明细ID
     * @return 结果
     */
    @Override
    public int deleteTbPriceStoreById(Long priceId)
    {
        return tbPriceStoreMapper.deleteTbPriceStoreById(priceId);
    }

    @Override
    public void updateTbPriceStoreAddQty(TbPriceStore tbPriceStore) {
        tbPriceStoreMapper.updateTbPriceStoreAddQty(tbPriceStore);
    }

    @Override
    public void updateTbPriceStoreReduceQty(TbPriceStore tbPriceStore) {
        tbPriceStoreMapper.updateTbPriceStoreReduceQty(tbPriceStore);
    }
}
